package com.chen;

import com.chen.mapper.CustomerMapper;
import com.chen.po.Customer;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static org.apache.ibatis.io.Resources.getResourceAsStream;


public class Main {
    /*
    根据id查询用户
     */
    @Test
    public void findCustomerById() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//    2.获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);

        Customer customer = customerMapper.findCustomerById(2);
        System.out.println(customer);
        sqlSession.close();
    }
    /*
    根据name模糊查询
     */
    @Test
    public void findCustomerByName() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//    2.获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
        //根据name查
        List<Customer> customers = customerMapper.findCustomerByName("j");
        for (Customer customer1: customers){
            System.out.println(customer1);
        }
        sqlSession.close();
    }
    /*
    新增用户
     */
    @Test
    public void AddCustomer() throws IOException {
        String name="uid";
        String job="teacher";
        String phone="13874247446";
        Customer customer = new Customer();
        customer.setUsername(name);
        customer.setJobs(job);
        customer.setPhone(phone);
        String resource = "mybatis-config.xml";
        InputStream inputStream = getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//    2.获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
        customerMapper.AddCustomer(customer);
        sqlSession.commit();
        sqlSession.close();
    }
    /*
    根据id更新用户信息
     */
    @Test
    public void updateCustomer() throws IOException {
        int id=1;
        String name="chen";
        String jobs="it";
        String phone="15879856545";
        Customer customer = new Customer();
        customer.setUsername(name);
        customer.setJobs(jobs);
        customer.setPhone(phone);
        customer.setId(id);
        String resource = "mybatis-config.xml";
        InputStream inputStream = getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//    2.获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
        int count = customerMapper.updateCustomer(customer);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
    }
    /*
    根据id删除用户
     */
    @Test
    public void deleteCustomer() throws IOException {
        int id=1;
        String resource = "mybatis-config.xml";
        InputStream inputStream = getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//    2.获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
        customerMapper.deleteCustomer(id);
        sqlSession.commit();
        sqlSession.close();
    }
    }